package com.darchrow.transaction;

import com.alibaba.fastjson.JSON;
import com.darchrow.pojo.Order;
import com.darchrow.service.StorageService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author mdl
 * @date 2020/4/27 15:27
 */
@Slf4j
@Component
@RocketMQMessageListener(consumerGroup = "producer_group_tx1", topic = "mq-trans-topic")
class ConsumerTransactionListener implements RocketMQListener<String> {

    @Resource
    private StorageService storageService;

    @Override
    public void onMessage(String message) {
        log.info("开始消费消息:{}", message);
        //解析消息
        Order order = JSON.parseObject(message, Order.class);

        //扣减库存
        storageService.deduct(order.getCommodityCode(), order.getCount(), order.getTxNum());
    }
}
